package com.erp.module.jiaFang.xGJiaFang

import android.app.Activity
import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.widget.ArrayAdapter
import com.erp.activity.BaseActivity
import com.erp.activity.MainKActivity
import com.erp.myAdapter.JiaFangXiangXIAdapter
import com.erp.bean.JiaFangDXXBean
import com.erp.mclass.QuanXian
import com.erp.mclass.UnCeHandler
import com.erp.R
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_jiafang.*
import kotlinx.android.synthetic.main.header_knoya.*

/**
 * Created by Administrator on 2018/7/2/002.
 * 作者：刘洋
 */
class JiaFangKActivity: BaseActivity(), JiaFangKView, View.OnClickListener {

    private val a = "JiaFangKActivity"
    var isgai = false
    lateinit var bean : JiaFangDXXBean
    lateinit var KP : JiaFangKP
    lateinit var updatedialog: AlertDialog
    lateinit var jiafangadapter: JiaFangXiangXIAdapter
    var id =""
    var inum =""
    override fun getLayoutId(): Int {
        activityname=a
        return R.layout.activity_jiafang
    }

    override fun initCreate() {
        UnCeHandler.buzhou = "initCreate"
        bean= JiaFangDXXBean()
        id=intent.getStringExtra("id")
        inum=intent.getStringExtra("inum")
        bean.id=id
        bean.inum=inum
        iv_header_left.visibility=View.VISIBLE
        iv_header_left.setOnClickListener(this)
        tv_header_title.text=inum
        BtShenHe.setOnClickListener(this)
        BtBaoCun.setOnClickListener(this)
        BtDelete.setOnClickListener(this)
        jiafangadapter = JiaFangXiangXIAdapter(this, bean.jiafanglist)
        LvJiaFang.adapter = jiafangadapter
        val listsp = arrayOf("华标三区仓","其它材料仓","五金仓","原料仓","中间仓","专卖店用品仓","饰品仓")
        val adapter = ArrayAdapter(this@JiaFangKActivity, R.layout.lv_sp_tvitem, listsp)
        SpCangKu.adapter = adapter
        val cangsp = arrayOf("出仓单","入仓单")
        val lxadapter = ArrayAdapter(this@JiaFangKActivity, R.layout.lv_sp_tvitem, cangsp)
        SpLeiXing.adapter = lxadapter
        KP = IJiaFangKP(this, this, id, bean)
        updatedialog = Myutil.showupdatedialog(this, "正在获取家纺单数据", false)
        KP.getJiaFangD()
    }
    fun initView(){
        UnCeHandler.buzhou = "initView"
        tv_header_title.text=bean.inum
        jiafangadapter.notifyDataSetChanged()
        Myutil.settv(Myutil.getdata(bean.audit_time),TvShenHeTime)
        Myutil.settv(Myutil.getdata(bean.ctime),TvZhiDanTime)
        Myutil.settv(Myutil.getdata(bean.auditor_name),TvShenHeName)
        Myutil.settv(Myutil.getdata(bean.creator_name),TvZhiDanName)
        Myutil.setet(bean.remark,EtBeiZhu)
        if(bean.auditor=="0"){
            BtShenHe.text="审核"
            BtBaoCun.visibility=View.VISIBLE
            BtDelete.visibility=View.VISIBLE
        }else {
            Log.d("TAAG","au="+bean.auditor)
            BtShenHe.text="取消审核"
            BtBaoCun.visibility=View.GONE
            BtDelete.visibility=View.GONE
        }
        if(QuanXian.isAuditJiaFang){
            BtShenHe.visibility = View.VISIBLE
        }else{
            BtShenHe.visibility=View.GONE
        }
        if(MainKActivity.uid !=bean.creator){
            if(!QuanXian.isEditAllJiaFang) {
                BtBaoCun.visibility=View.GONE
            }else {
                BtBaoCun.visibility=View.VISIBLE
            }
        }
        if(bean.audit_anyway=="0"){
            CbChaoGuo.isChecked=false
        }else if(bean.audit_anyway=="1"){
            CbChaoGuo.isChecked=true
        }

        when(bean.type){
            "-1" -> SpLeiXing.setSelection(0)
            "1" -> SpLeiXing.setSelection(1)
        }
        when(bean.iw_id){
            "1" -> SpCangKu.setSelection(0)
            "2" -> SpCangKu.setSelection(1)
            "3" -> SpCangKu.setSelection(2)
            "4" -> SpCangKu.setSelection(3)
            "5" -> SpCangKu.setSelection(4)
            "6" -> SpCangKu.setSelection(5)
            "7" -> SpCangKu.setSelection(6)
        }
    }
    override fun onClick(v: View?) {
        UnCeHandler.buzhou = "onClick"
        when(v){
            iv_header_left -> {
                fin()
            }
            BtShenHe -> {
                updatedialog = Myutil.showupdatedialog(this, "正在审核", false)
                if(CbChaoGuo.isChecked){
                    bean.audit_anyway="1"
                }else{
                    bean.audit_anyway="0"
                }
                KP.shenHeJiaFang()
            }
            BtDelete -> {
                updatedialog = Myutil.showupdatedialog(this, "正在删除", false)
                val dialog = AlertDialog.Builder(this@JiaFangKActivity)
                dialog.setTitle("提示")
                dialog.setMessage("确定要删除     " + bean.inum + "     这条质量问题?")
                dialog.setPositiveButton("确定") { dialog, which ->
                    updatedialog = Myutil.showupdatedialog(this@JiaFangKActivity, "正在删除", false)
                    KP.deleteJiaFang()
                }
                dialog.setNegativeButton("取消") { dialog, _ -> dialog.dismiss() }
                dialog.show()
            }
            BtBaoCun -> {
                updatedialog = Myutil.showupdatedialog(this, "正在保存", false)
                bean.remark=EtBeiZhu.text.toString()
                KP.baoCunJiaFang(SpCangKu.selectedItem.toString(),SpLeiXing.selectedItem.toString())
            }
        }
    }

    override fun shenHeJiaFangOk() {
        updatedialog.dismiss()
        initView()
    }

    override fun shenHeJiaFangNo() {
        updatedialog.dismiss()
    }

    override fun baoCunJiaFangOk() {
        isgai=true
        updatedialog.dismiss()
        initView()
    }

    override fun baoCunJiaFangNo() {
        updatedialog.dismiss()
    }

    override fun deleteJiaFangOk() {
        isgai=true
        updatedialog.dismiss()
        fin()
    }

    override fun deleteJiaFangNo() {
        updatedialog.dismiss()
    }

    override fun getJiaFangDOk() {
        isgai=true
        updatedialog.dismiss()
        initView()
    }

    override fun getJiaFangDNo() {
        updatedialog.dismiss()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if(keyCode==4){
            fin()
        }
        return super.onKeyDown(keyCode, event)
    }
    fun fin(){
        UnCeHandler.buzhou = "fin"
        if(isgai){
            val intent = Intent()
            setResult(Activity.RESULT_OK, intent)
            finish()
        }else{
            finish()
        }
    }
}